#include "Skydome.h"
#ifdef WIN_32
#include "esUtil.h"
#endif
#include <GLES2/gl2.h>
#include "cGame_Load.h"
#include "Engine.h"
#include "Camera.h"

extern cGame_Load* cgame_Load;
extern Camera camera;

GLfloat vVertices[] = {1.0,-1.0,-1.0,
						1.0,1.0,-1.0,
						-1.0,1.0,-1.0,
						-1.0,-1.0,-1.0,
						1.0,-1.0,1.0,
						1.0,1.0,1.0,
						-1.0,1.0,1.0,
						-1.0,-1.0,1.0													
						};   

   
  
GLbyte indices[] = {0,1,2,0,2,3,
				0,4,5,0,5,1,
				0,7,4,0,3,7,
				6,3,2,6,7,3,
				6,4,7,6,5,4,
				6,2,1,6,1,5};

Skydome::Skydome()
{
	this->texture = cgame_Load->texture3D[0];
}

Skydome::~Skydome()
{
}

void Skydome::Render()
{	

	Matrix pvmMatrix, viewProjection, wvpMatrix, *projectionMatrix;    
	glViewport(0, 0, SCREEN_W, SCREEN_H);
	// ProjectionMatrix goes here.
	viewProjection.LoadPerperstive(45, SCREEN_W / (float) SCREEN_H, 0.1f, 200.0f);

	viewProjection.RotateX(-camera.rotation.x);
	viewProjection.RotateY(-camera.rotation.y);
	viewProjection.RotateZ(-camera.rotation.z);    
	wvpMatrix.Scale(6, 6, 6);
	// Mutiply with View Matrix  (Camera Matrix)
	projectionMatrix = this->GetTransform();
	pvmMatrix = *projectionMatrix*viewProjection*wvpMatrix;
	
	glUseProgram(Uniforms::programCube);

	glUniformMatrix4fv(Uniforms::uCubeMVPMatrix, 1, GL_FALSE, pvmMatrix.value);        
	// Render the Object
			
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
	glUniform1i(Uniforms::uCubeSampler, 0);
    
	glVertexAttribPointer(ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
	glEnableVertexAttribArray(ATTRIB_POSITION);
      
	glDrawElements(GL_TRIANGLES, sizeof(indices), GL_UNSIGNED_BYTE, indices); 
}

